#include <linux/backing-dev.h>
#include <linux/init.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/freezer.h>
#include <linux/sched/signal.h>
#include <linux/slab_def.h>
#include <linux/random.h>

#include "dbcache.h"
#include "hc.h"
#include "kmeans.h"
#include "util_db.h"

/**
 * 左闭右闭区间：[hotness_ext_start, hotness_ext_end]
*/
void hotness_ext(struct bio *bio, unsigned int *hotness_ext_start, unsigned int *hotness_ext_end) {
    sector_t sec_start;
    sector_t sec_count;
    sector_t sec_end;

    sec_start = bio->bi_iter.bi_sector;
    sec_count = bio_sectors(bio);
    sec_end = sec_start + sec_count - 1;
    *hotness_ext_start = sec_start / HOT_SECS;
    *hotness_ext_end = sec_end / HOT_SECS;
}

static int bch_hc_thread(void *arg) {
	int err;
	unsigned int wait_ms;
    struct cached_dev *dc = arg;
    struct dbcache_hc_kthread *hc_th = dc->hc->hc_thread;
    wait_queue_head_t *wq = &dc->hc->hc_thread->hc_wait_queue_head;
	wait_ms = hc_th->sleep_time;

	set_freezable();
	do {
		wait_event_interruptible_timeout(*wq, kthread_should_stop() || freezing(current), msecs_to_jiffies(wait_ms));
        // printk("calling func kmeans_cluster.\n");
		err = kmeans_cluster(dc);
		if (!err) dc->hc->centers_valid = 1;
	} while (!kthread_should_stop());
	return 0;
}

int bch_hc_thread_start(struct cached_dev *dc) {
    FUNC_SHOUT();
    struct dbcache_hc_kthread *hc_th;
	int err = 0;

    hc_th = kmalloc(sizeof(struct dbcache_hc_kthread), GFP_KERNEL);
    if (!hc_th) {
		err = -ENOMEM;
		goto out;
	}

    hc_th->sleep_time = DEF_HC_THREAD_SLEEP_TIME;

    init_waitqueue_head(&hc_th->hc_wait_queue_head);
    hc_th->dbcache_hc_task = kthread_run(bch_hc_thread, dc, "dbcache_hc");
    if (IS_ERR(hc_th->dbcache_hc_task)) {
		err = PTR_ERR(hc_th->dbcache_hc_task);
		kfree(hc_th);
	}

    dc->hc->hc_thread = hc_th;
    
out:
	return err;
}

void bch_hc_thread_stop(struct cached_dev *dc) {
    FUNC_SHOUT();
    struct dbcache_hc_kthread *hc_th = dc->hc->hc_thread;
	if (!hc_th)
		return;
	kthread_stop(hc_th->dbcache_hc_task);
	kfree(hc_th);
	dc->hc->hc_thread = NULL;
}

int hc_init(struct cached_dev *dc) {
    struct hotness_cluster *hc;
    __u64 i;
    __u64 hotness_nr;
    int ret;

    hc = kmalloc(sizeof(struct hotness_cluster), GFP_KERNEL);
    if (!hc)
		goto err;

    hotness_nr = bdev_sectors(dc->bdev) / HOT_SECS;
    if (bdev_sectors(dc->bdev) % HOT_SECS)
        hotness_nr++;
    printk("%s: size of hotness_array is %llu.\n", __func__, hotness_nr);
    hc->hotness_nr = hotness_nr;
    hc->count = 0;
    hc->hotness_array = kmalloc_array(hotness_nr, sizeof(hotness_t), GFP_KERNEL);
    if (!hc->hotness_array) 
        goto err;
    for (i = 0; i < hotness_nr; i++) {
        hc->hotness_array[i] = 0;
    }

    for (i = 0; i < N_CLUSTER; i++) {
        hc->centers[i] = 0;
    }
    hc->centers_valid = 0;

    dc->hc = hc;
    ret = 0;

out:
	return ret;

err:
	pr_info("error in func %s", __func__);
    ret = -1;
	goto out;
}

void hc_release(struct cached_dev *dc) {
    printk("%s: here!\n", __func__);

    kfree(dc->hc->hotness_array);
    kfree(dc->hc);
}
